﻿// ------------------------------------------------------------------------
//    XBMControl - A compact remote controller for XBMC (.NET 3.5)
//    Copyright (C) 2008  Bram van Oploo (bramvano@gmail.com)
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
// ------------------------------------------------------------------------

using System;
using System.IO;
using System.Drawing;
using System.Net;
using System.Windows;
using System.Windows.Media.Imaging;

namespace XBMC
{
    public class XbmcNowPlaying
    {
        readonly XbmcCommunicator _parent;
        private string[,] _maNowPlayingInfo;
        //private string _error;

        public XbmcNowPlaying(XbmcCommunicator p)
        {
            _parent = p;
        }

        public string Get(string field, bool refresh)
        {
            string returnValue = null;

            if (refresh)
            {
                string[] aNowPlayingTemp = _parent.Request("GetCurrentlyPlaying");

                if (aNowPlayingTemp != null)
                {
                    _maNowPlayingInfo = new string[aNowPlayingTemp.Length, 2];
                    for (int x = 0; x < aNowPlayingTemp.Length; x++)
                    {
                        int splitIndex = aNowPlayingTemp[x].IndexOf(':') + 1;

                        if (splitIndex > 2)
                        {
                            _maNowPlayingInfo[x, 0] = aNowPlayingTemp[x].Substring(0, splitIndex - 1).Replace(" ", "").ToLower();
                            _maNowPlayingInfo[x, 1] = aNowPlayingTemp[x].Substring(splitIndex, aNowPlayingTemp[x].Length - splitIndex);
                            
                            if (_maNowPlayingInfo[x, 0] == field)
                                returnValue = _maNowPlayingInfo[x, 1];
                        }
                    }
                }
            }
            else
            {
                for (var x = 0; x < _maNowPlayingInfo.GetLength(0); x++)
                {
                    if (_maNowPlayingInfo[x, 0] == field)
                        returnValue = _maNowPlayingInfo[x, 1];
                }
            }

            return returnValue;
        }

        public string Get(string field)
        {
            return Get(field, false);
        }

        public BitmapSource GetCoverArt()
        {
            BitmapSource bitmapSource = null;
            var client = new WebClient();
            var xbmcThumbUri = new Uri("http://" + _parent.GetIp() + "/thumb.jpg");
            _parent.Request("GetCurrentlyPlaying", @"special:\\xbmc\web\thumb.jpg");

            try
            {
                var stream = new MemoryStream(client.DownloadData(xbmcThumbUri));
                var thumbnail = new Bitmap(Image.FromStream(stream));
                bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                    thumbnail.GetHbitmap(),
                    IntPtr.Zero,
                    Int32Rect.Empty,
                    BitmapSizeOptions.FromEmptyOptions());
                return bitmapSource;
            }
            catch (Exception)
            {
                //_error = e.Message;
                return null;
            }
            finally
            {
                client.Dispose();
            }
            return bitmapSource;
        }

        public string GetMediaType()
        {
            return Get("type", true);
        }
    }
}
